package com.cloudkinto.common.utils;

import com.cloudkinto.common.common.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Author Administrator
 *
 * @Des
 * @Version 2019/6/3
 */
public class TimeUtils {
    public static String YYYYMMDD = "yyyy-MM-dd";
    public static final String YYYYMMDDSLASH = "yyyy/MM/dd";
    public static final String YYYYMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static String YYYYMMDDChinese = "yyyy年MM月dd日";
    public static String MMDD = "MM-dd";
    public static String MMDDSLASH = "MM/dd";
    public static String MMDDChinese = "MM月dd日";

    /**
     * @return yyyy-MM-dd
     */
    public static String getCurrentDateFormat() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(new Date());
    }

    /**
     * @return format
     */
    public static String getCurrentDateFormat(String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(new Date());
    }

    /**
     * @return format
     */
    public static String getCurrentDateFormat(Date date, int months, int days) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, months);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * @return format
     */
    public static String getAfterTimeFormat(String format, int hours, int mins) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, hours);
        calendar.add(Calendar.MINUTE, mins);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * @return yyyy-MM-dd
     */
    public static String getCurrentDateFormat(int addDays) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, addDays);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(calendar.getTime());
    }    public static String getCurrentDateFormat(int addDays,String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, addDays);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(calendar.getTime());
    }


    public static String getMMddDate(int addDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, addDay);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        return dateFormat.format(calendar.getTime());
    }

    public static String getddDate(int addDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, addDay);
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd");
        return dateFormat.format(calendar.getTime());
    }

    public static String getYYYYMMDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        return dateFormat.format(new Date());
    }

    public static String getYYYYMMDate(int addMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, addMonth);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        return dateFormat.format(new Date());
    }

    public static String getYYYYMMDate(int addMonth, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, addMonth);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(new Date());
    }

    public static String getCurrentDateTimeFormat() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(new Date());
    }

    /**
     * "yyyy-MM-dd" 格式
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    /**
     * "yyyy-MM-dd" 格式
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * 把时间转换时区
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date, String format, String timeZone) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone)); // 设置时区为GMT
        return dateFormat.format(date);
    }

    /**
     * "yyyy-MM-dd"
     *
     * @param date
     * @return
     */
    public static Date parseDate(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

    /**
     * "yyyy-MM-dd"
     *
     * @param date
     * @return
     */
    public static Date parseDate(String date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

    public static String formatTime(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        return dateFormat.format(date);
    }

    /**
     * 多格式格式化字符串日期
     * <p>
     * YYYYMMDDSLASH = "yyyy/MM/dd";
     * YYYYMMDDChinese = "yyyy年MM月dd日";
     * MMDD = "MM-dd";
     * MMDDSLASH = "MM/dd";
     * MMDDChinese = "MM月dd日";
     *
     * @param dateStr
     * @return
     */
    public static String parseDateMuti(String dateStr) {
        SimpleDateFormat dateFormat1 = new SimpleDateFormat(MMDDSLASH);
        SimpleDateFormat dateFormat2 = new SimpleDateFormat(MMDDChinese);
        SimpleDateFormat dateFormat3 = new SimpleDateFormat(MMDD);
        SimpleDateFormat dateFormat4 = new SimpleDateFormat(YYYYMMDDSLASH);
        SimpleDateFormat dateFormat5 = new SimpleDateFormat(YYYYMMDDChinese);
        Date date = new Date();
        try {
            date = dateFormat1.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            try {
                date = dateFormat2.parse(dateStr);
            } catch (ParseException ex) {
                ex.printStackTrace();
                try {
                    date = dateFormat3.parse(dateStr);
                } catch (ParseException exc) {
                    exc.printStackTrace();
                    try {
                        date = dateFormat4.parse(dateStr);
                    } catch (ParseException e1) {
                        e1.printStackTrace();
                        try {
                            date = dateFormat5.parse(dateStr);
                        } catch (ParseException e2) {
                            e2.printStackTrace();
                        }
                    }
                }
            }
        }
        return formatDate(date);
    }

    public static Date getDateAdd(Date date, int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, i);
        return calendar.getTime();
    }

    public static String formatDateMMdd(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        return dateFormat.format(date);
    }

    /**
     * 获取上个月的日期
     *
     * @return
     */
    public static String getLastMonthDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        return dateFormat.format(calendar.getTime());
    }

    public static int getBetweenDays(Date parseDate) {
        long timeBetweenMillins = System.currentTimeMillis() - parseDate.getTime();
        return (int) timeBetweenMillins / 1000 / 60 / 60 / 24;
    }
    public static int getBetweenDays(String dayStart, String dayEnd) {
        long timeBetweenMillins = TimeUtils.parseDate(dayEnd).getTime() - TimeUtils.parseDate(dayStart).getTime();
        return (int) (timeBetweenMillins / 1000 / 60 / 60 / 24);
    }
    /**
     * 2007-10-23T17:15:44.000Z 根据这个字符串放回Date类型的时间
     *
     * @param time
     * @return
     */
    public static Date getDateTime(String time) {
        try {
            time = time.replace("Z", " UTC");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dt = sdf.parse(time);
            return dt;
        } catch (Exception e) {
            return new Date();
        }
    }

    /**
     * 2007-10-23T17:15:44Z 根据这个字符串放回Date类型的时间
     *
     * @param time
     * @return
     */
    public static Date getDateTime2(String time) {
        try {
            time = time.replace("Z", " UTC");
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z");
            Date dt = sdfDate.parse(time);
            return dt;
        } catch (Exception e) {
            return new Date();
        }
    }

    public static String addDaysFormat(String dateStr, String format, int days) {
        Date date = TimeUtils.parseDate(dateStr, format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return TimeUtils.formatDate(calendar.getTime(), format);
    }


    /**
     * "yyyy-MM-dd" 格式
     *
     * @param date
     * @return
     */
    public static Date formatDateToDate(Date date, String format, String timeZone) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone)); // 设置时区为GMT
        return DateUtils.stringFormatToDate(dateFormat.format(date), format);
    }

}
